/*****************************************************************************
 * video_widget.cc : Gtkmmm interface
 ****************************************************************************
 * Copyright © 2006-2008 the VideoLAN team
 *
 * Authors: David Guglielmi <raxxx.fr@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include <gtkmm/calendar.h>

#include "video_widget.hh"

#include <vlc_vout.h>

VideoWidget::VideoWidget (intf_thread_t *_p_i) : p_intf (_p_i)
{

  Glib::Date date;
  Gtk::Calendar ().get_date (date);

  if (date.get_day_of_year () >= 354)
    set_pixbuf (Gdk::Pixbuf::create_from_file (Glib::ustring (config_GetDataDir ()) + "/vlc128x128-christmas.png"));
  else
      set_pixbuf (Gdk::Pixbuf::create_from_file (Glib::ustring (config_GetDataDir ()) + "/vlc128x128.png"));


}

VideoWidget::~VideoWidget ()
{

  vlc_mutex_lock (&lock);

  if (p_vout)
   {
    if (!p_intf->psz_switch_intf)
     {
      if (vout_Control (p_vout, VOUT_CLOSE) != VLC_SUCCESS)
        vout_Control (p_vout, VOUT_REPARENT);
     }
    else
     {
      if (vout_Control (p_vout, VOUT_REPARENT) != VLC_SUCCESS)
        vout_Control (p_vout, VOUT_CLOSE);
     }
  }

  vlc_mutex_unlock (&lock);
  vlc_mutex_destroy (&lock);

}

void*
VideoWidget::request (vout_thread_t *_p_vout, int *pi_x, int *pi_y, unsigned int *pi_width, unsigned int *pi_height)
{

  if (p_vout)
   {
    msg_Dbg (p_intf, "embedded video already in use");
    return NULL;
   }
  
  p_vout = _p_vout;

  set_preferred_size (pi_width[0], pi_height[0]);

  return (void*)get_xid ();

}

int
VideoWidget::control (void *p_window, int i_query, va_list args)
{

    int i_ret = VLC_SUCCESS;
    switch( i_query )
    {
        case VOUT_GET_SIZE:
        {
            unsigned int *pi_width  = va_arg( args, unsigned int * );
            unsigned int *pi_height = va_arg( args, unsigned int * );
            *pi_width = get_width ();
            *pi_height = get_height ();
            break;
        }
        case VOUT_SET_SIZE:
        {
            unsigned int i_width  = va_arg( args, unsigned int );
            unsigned int i_height = va_arg( args, unsigned int );
            set_preferred_size (i_width, i_height);
            //emit askVideoToResize( i_width, i_height );
            //emit askUpdate();
            break;
        }
        case VOUT_SET_STAY_ON_TOP:
        {
            //int i_arg = va_arg( args, int );
            //QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
            break;
        }
        default:
            i_ret = VLC_EGENERIC;
            msg_Warn( p_intf, "unsupported control query" );
            break;
    }


  //std::cout << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl;
    
    return i_ret;

}

void
VideoWidget::release (void *p_win)
{

  p_vout = NULL;

}


